package com.xiangyueEducation.uploaderCloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    @Bean(name = "ioPool")
    public TaskExecutor ioTaskExecutor() {
        // 获取系统的核心数量
        int core = Runtime.getRuntime().availableProcessors();
        int corePoolSize = core*2;

        // 创建线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数为核心数的两倍
        executor.setCorePoolSize(corePoolSize);

        // 设置最大线程数为核心线程数
        executor.setMaxPoolSize(corePoolSize);
        
        // 设置队列长度为200
        executor.setQueueCapacity(2000);
        
        // 设置线程名称前缀
        executor.setThreadNamePrefix("IO-Executor-");

        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 初始化线程池
        executor.initialize();
        
        return executor;
    }
}
